﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using PaDgame;

public class FirstController : MonoBehaviour, ISceneController, IUserAction
{
	public BankController fromBank;
	public BankController toBank;
	public BoatController boat;
	private MyCharacterController[] characters;
	UserGUI userGUI;

	public CCActionManager actionManager;
	public JudgeManager judgeManager;

	void Awake()
	{
		SSDirector director = SSDirector.getInstance();
		director.currentSceneController = this;
		characters = new MyCharacterController[6];
		userGUI = gameObject.AddComponent<UserGUI>() as UserGUI;
		loadResources();

		actionManager = gameObject.AddComponent<CCActionManager>() as CCActionManager;
		judgeManager = gameObject.AddComponent<JudgeManager>() as JudgeManager;
	}

	public void loadResources()
	{
		GameObject river = Instantiate(Resources.Load("Prefabs/river", typeof(GameObject)), new Vector3(0, 0.25F, 5F), Quaternion.identity, null) as GameObject;
		river.name = "river";

		fromBank = new BankController("from");
		toBank = new BankController("to");
		boat = new BoatController();

		loadCharacter();
	}

	private void loadCharacter()
	{
		for (int i = 0; i < 3; i++)
		{
			MyCharacterController chCtrl = new MyCharacterController("priest");
			chCtrl.setName("priest" + i);
			chCtrl.setPosition(fromBank.getEmptyPosition());
			chCtrl.getOffBoat(fromBank);
			fromBank.getOffBoat(chCtrl);
			characters[i] = chCtrl;
		}

		for (int i = 0; i < 3; i++)
		{
			MyCharacterController chCtrl = new MyCharacterController("devil");
			chCtrl.setName("devil" + i);
			chCtrl.setPosition(fromBank.getEmptyPosition());
			chCtrl.getOffBoat(fromBank);
			fromBank.getOffBoat(chCtrl);

			characters[i + 3] = chCtrl;
		}
	}


	public void moveBoat()
	{
		if (boat.isEmpty())
			return;
		//boat.boatMove();

		actionManager.moveto(boat.getBoat(),boat.getBoatDestination(),boat.getMoveSpeed());

		userGUI.status = judgeManager.judgeWinOrLose();
	}

	public void clickCharacter(MyCharacterController chCtrl)
	{
		if (chCtrl.getIsOnBoat())
		{
			BankController whichBank;
			if (boat.getToOrFrom() == -1)
			{
				whichBank = toBank;
			}
			else
			{
				whichBank = fromBank;
			}

			boat.getOffBoat(chCtrl.getName());
			//chCtrl.moveToDestination(whichBank.getEmptyPosition());
			actionManager.moveto(chCtrl.getCharacter(), whichBank.getEmptyPosition(), chCtrl.getMoveSpeed());
			chCtrl.getOffBoat(whichBank);
			whichBank.getOffBoat(chCtrl);
		}
		else
		{
			BankController whichBank = chCtrl.getBankController();

			if (boat.getEmptyIndex() == -1)
			{
				return;
			}

			if (whichBank.getToOrFrom() != boat.getToOrFrom())
				return;

			whichBank.getOnBoat(chCtrl.getName());
			//chCtrl.moveToDestination(boat.getEmptyPosition());
			actionManager.moveto(chCtrl.getCharacter(), boat.getEmptyPosition(), chCtrl.getMoveSpeed());
			chCtrl.getOnBoat(boat);
			boat.getOnBoat(chCtrl);
		}
		userGUI.status = judgeManager.judgeWinOrLose();
	}
	/*
	int judgeWinOrLose()
	{
		int fromPriest = 0;
		int fromDevil = 0;
		int toPriest = 0;
		int toDevil = 0;

		int[] fromCount = fromBank.getCharacterNum();
		fromPriest += fromCount[0];
		fromDevil += fromCount[1];

		int[] toCount = toBank.getCharacterNum();
		toPriest += toCount[0];
		toDevil += toCount[1];

		if (toPriest == 3 && toDevil == 3)
			return 2;

		int[] boatCount = boat.getCharacterNum();
		if (boat.getToOrFrom() == -1)
		{
			toPriest += boatCount[0];
			toDevil += boatCount[1];
		}
		else
		{
			fromPriest += boatCount[0];
			fromDevil += boatCount[1];
		}
		if (fromPriest < fromDevil && fromPriest > 0)
		{
			return 1;
		}
		if (toPriest < toDevil && toPriest > 0)
		{
			return 1;
		}
		return 0;
	}
	*/
	public void restart()
	{
		boat.reset();
		fromBank.reset();
		toBank.reset();
		for (int i = 0; i < characters.Length; i++)
		{
			characters[i].reset();
		}
	}
}